using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Models;
using ToDoListWeb.Service;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace ToDoListWeb.Controllers
{
    public class UserController : Controller
    {
        UserService _userService = new UserService();
        MatterService _matterService = new MatterService();

        [HttpGet]
        public IActionResult Index()
        {
            if (HttpContext.Session.GetInt32("userId") == null)
                return View("Login");
            else
                return RedirectToAction("ShowEvents");
        }
        /// <summary>
        /// 界面：浏览所有清单、事项
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowEvents()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }
        /// <summary>
        /// 在布局页中显示用户昵称等信息，若昵称不存在，
        /// 则显示用户名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuInfo()
        {
            _userService.UpdateUserModel(GetUser());
            var menuModel = _userService.GetMenuInfoModel(GetUser());
            return View(menuModel);
        }
        /// <summary>
        /// 用户界面：显示用户信息及更改用户昵称
        /// </summary>
        /// <returns></returns>
        public IActionResult UserInfo()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }
        /// <summary>
        /// 修改昵称
        /// </summary>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public IActionResult ChangeNickName(string nickName)
        {
            if (nickName != null)
                _userService.ChangeNickName(GetUser().UserId, nickName);
            return View("ShowEvents", GetUser());
        }
        /// <summary>
        /// 注销用户
        /// </summary>
        /// <returns></returns>
        public IActionResult Logout()
        {
            HttpContext.Session.Clear();
            return RedirectToAction("Index");
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(string userName, string password)
        {
            var validUser = _userService.Login(userName, password);
            if (!validUser)
            {
                ViewData["Message"] = "你的用户名或密码错误";
                return View();
            }
            else
            {
                HttpContext.Session.SetInt32("userId", _userService.GetUser(userName, password).UserId);
                return View("ShowEvents", GetUser());
            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult Register()
        {
            return View("Register");
        }
        /// <summary>
        ///注册用户 
        /// </summary>
        /// <param name="newUserName"></param>
        /// <param name="password"></param>
        /// <param name="rePassword"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateData(string newUserName, string password, string rePassword, string nickName)
        {
            if (password == null || newUserName == null || rePassword == null)
            {
                ViewData["Message"] = "输入不能为空";
                return View("Register");
            }
            else if (password.Equals(rePassword))
            {
                if (_userService.Register(newUserName, password, nickName))
                    return View("Login");
                else
                {
                    ViewData["Message"] = "你输入用户名已存在";
                    return View("Register");
                }
            }
            else
            {
                ViewData["Message"] = "你输入的两次密码不一致";
                return View("Register");
            }
        }

        UserModel GetUser()
        {
            int userId = (int)HttpContext.Session.GetInt32("userId");
            return _userService.GetUserModel(userId);
        }
    }
}